/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sun.hunk.struts;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author sunil
 */
public class HunkActionForm extends ActionForm{

    private String button;
    private Collection<String> errors = null;
    private Collection<String> fields = null;

    public String getButton() {
        return button;
    }

    public void setButton(String button) {
        this.button = button;
    }

    protected void addError(String error) {
        errors.add(error);
    }

    protected void addError(String field,String error) {
        fields.add(field);
        errors.add(error);
    }

    public Collection<String> getErrors() {
        return errors;
    }

    public void setErrors(Set<String> errors) {
        this.errors = errors;
    }

    public Collection<String> getFields() {
        return fields;
    }

    public void setFields(Set<String> fields) {
        this.fields = fields;
    }

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {    
        ActionErrors actionErrors = new ActionErrors();
        if(errors.size() > 0){
            ActionMessage message = new ActionMessage("DeFAUlT ErRoR");
            actionErrors.add("work", message);
            request.setAttribute("errors", errors);         
            request.setAttribute("fields", fields);
        }
        return actionErrors;
    }    

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        super.reset(mapping, request);
        errors = new HashSet<String>();
        fields = new HashSet<String>();
    }

}
